<?php
class user_model extends CI_Model
{
    /**
     * user or member list 
     */
    public function totalUser()
    {
        $sql = "SELECT count(*) as total FROM users";
        $query =  $this->db->query($sql);
        $result = $query->row_array();
        return $result['total'];
    }
    public function abc()
    {
        $limit = 30;
        $this->db->select('u.id, u.fullname, u.username, u.status, ur.rid, r.name as role_name');
        $this->db->from('users u');
        $this->db->join('users_roles ur', 'ur.uid = u.id');
        $this->db->join('role r', 'r.rid = ur.rid');
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        $result = $query->result_array();
        //$query = $this->db->get_where("users", null, $limit, $offset);
        $paging = paging_admin("users/index", $this->totalUser(), $limit);
        return array("query" => $result, "paging" => $paging);
    }
    public function userList($offset)
    {
        $limit = 30;
        $this->db->select('u.id, u.firtname, u.lastname, u.username, u.status');
        $this->db->from('users u');
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        $result = $query->result_array();
        //$query = $this->db->get_where("users", null, $limit, $offset);
        $paging = paging_admin("users/index", $this->totalUser(), $limit);
        return array("query" => $result, "paging" => $paging);
    }
    public function GetUserById($id)
    {
         $sql = "SELECT *, GROUP_CONCAT(rid) as rolelist from (
            	SELECT `u`.*, `r`.`rid` 
            	FROM (`users` u) 
            	LEFT JOIN `users_roles` r ON `r`.`uid` = `u`.`id` 
            	WHERE `u`.`id` = ?)
            temp group by id";
        $query = $this->db->query($sql, array($id));
        return $query->row_array();
    }
    // checking
    public function isUser($id)
    {
        $query = $this->db->get_where('users', array('id' => $id));
        if ($query->num_rows() > 0)
            return true;
        return false;
    }

    public function checkForm()
    {
        $this->form_validation->set_rules("firtname", 'fistname', "trim|required");
        $this->form_validation->set_rules("lastname", 'lastname', "trim|required");
        return $this->form_validation->run();
    }
    public function checkFormAdd()
    {
        $this->form_validation->set_rules("username", 'username', "trim|required");
        $this->form_validation->set_rules("firtname", 'fistname', "trim|required");
        $this->form_validation->set_rules("lastname", 'lastname', "trim|required");
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('acti', '', 'trim');
        return $this->form_validation->run();
    }
    public function checkFormPass()
    {
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        return $this->form_validation->run();
    }
    public function doUpdate($params)
    {
        $this->load->helper('text');
        
        if (isset($_POST['idx'])) {
            $id = decrypt($_POST["idx"]);
            return $this->db->update('users', $params, array('id' => $id));
        }
    }
    public function doAdd($params)
    {
        $role = $params['role'];
        unset($params['role']);
        $this->load->helper('text');
        if ($this->db->insert('users', $params)) {
            $uid = $this->db->insert_id();
            return $this->addRole($uid, $role);
        }
        return 0;
    }
    public function addRole($uid, $arrRole)
    {
        $data = array();
        $i = 0;
        foreach ($arrRole as $rid) {
            $data[$i]['uid'] = $uid;
            $data[$i]['rid'] = $rid;
            $i++;
        }
        return $this->db->insert_batch('users_roles', $data);
    }
    public function changepass($params)
    {
        $field['pass'] = imd5(trim($params['password']));
        $id = decrypt($params['idx']);
        return $this->db->update('users', $field, array('id' => $id));
    }

    public function updateInfo($params)
    {
        $this->load->helper('text');
        $field['firtname'] = $params['firtname'];
        $field['lastname'] = $params['lastname'];
        if (trim($params['password']) != '') {
            $field['pass'] = imd5(trim($params['password']));    
        }
        $field['status'] = $params['status'];
        $id = decrypt($params['idx']);
        $this->db->update('users', $field, array('id' => $id));
    }
    public function deleteRole($userId) 
    {
        $this->db->delete('users_roles', array('uid' => $userId));
    }
    
    public function deleteUser($uid)
    {
        $arrRole = $this->auth_model->rid();
        if (in_array(3, $arrRole) && $this->auth_model->userid() != $uid) {
            return $this->db->delete('users', array('id' => $uid));
        }
    }       
}
